package com.mi.rof.rocketmq.comsumer;

import com.mi.rof.engineer.service.InventoryService;
import com.mi.rof.entity.InventoryApplyDTO;
import com.mi.rof.entity.SystemException;
import com.mi.rof.entity.enums.ExceptionEnum;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

import static com.mi.rof.utils.RocketMQTopic.APPLY_MATERIALS_TOPIC;

/**
 * @author : Yang Yu you
 */
@Component
@RocketMQMessageListener(consumerGroup = "consumer-group-apply", topic = APPLY_MATERIALS_TOPIC)
public class ApplyMaterialConsumer implements RocketMQListener<InventoryApplyDTO> {

    @Resource
    private InventoryService inventoryService;

    @Override
    public void onMessage(InventoryApplyDTO inventoryApplyDTO) {
        try {
            inventoryService.applyInventory(inventoryApplyDTO);
        }catch (SystemException e){
            throw new SystemException(ExceptionEnum.INVENTORY_NOT_ENOUGH.getCode(),
                    ExceptionEnum.INVENTORY_NOT_ENOUGH.getMsg());
        }
    }
}
